export function checkTimesValid(
  times: { end: number; color: string }[],
  timeout: { time: number; color: string },
) {
  if (!times || timeout.time === undefined || !timeout.color)
    return { valid: false, message: '参数错误' };
  if (timeout.time == 0) return { valid: true, message: '' };
  const len = times.length;
  if (len === 0) return { valid: false, message: '超时时间不为0时，时段不能为空' };
  if (times[len - 1].end != timeout.time)
    return { valid: false, message: '最后一个时间段的结束时间必须等于超时时间' };
  if (len > 1) {
    for (let i = 1; i < len; i++) {
      const { end: preEnd, color: preColor } = times[i - 1];
      const { end, color } = times[i];
      if (preEnd === undefined || !preColor || end === undefined || !color)
        return { valid: false, message: '时段参数错误' };
      if (end <= preEnd)
        return { valid: false, message: '时段结束时间必须大于上一个时段的结束时间' };
    }
  }
  return { valid: true, message: '' };
}

export function mapTimes(times: { end: number; start: number; color: string }[]) {
  if (!times) return [];
  return times.map(({ end, color }) => ({ end, color }));
}

export function mapSpotColor(spotColor: PDAColorConfig): PDAColorFormConfig {
  return {
    ...spotColor,
    times:
      spotColor?.times.map((item, index, array) => {
        if (index == 0)
          return {
            start: 0,
            ...item,
          };
        const prev = array[index - 1];
        return {
          start: prev.end,
          ...item,
        };
      }) || [],
  };
}
